﻿using System;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.CQRS.Events;
using Ue.CQRS.Utility;

namespace Ue.CQRS.Tests.Utility
{
    [TestClass]
    public class EventUtilityTests
    {
        public MethodInfo[] handlerMethods;

        [TestInitialize]
        public void Initial()
        {
            handlerMethods = typeof(FakeHandler).GetMethods();
        }

        #region EventHandleIsVerify

        [TestMethod]
        public void EventHandleIsVerify()
        {
            var methods = new[]{
                "Handler2",
                "Handler3"
            };

            foreach (var item in methods)
            {
                Assert.IsTrue(EventUtility.EventHandleIsVerify(this.GetMethod(item)));
            }

        }

        [TestMethod]
        public void EventHandleIsNotVerify()
        {
            var methods = new[]{
                "Handler",
                "Handler4"
            };

            foreach (var item in methods)
            {
                Assert.IsFalse(EventUtility.EventHandleIsVerify(this.GetMethod(item)));
            }
        }

        //[TestMethod]
        //public void MethodParameterGreaterThan2ShouldThrowException()
        //{
        //    MemberInfo method = new MethodInfo();
        //}

        #endregion

        #region private mehtods

        private MethodInfo GetMethod(string name)
        {
            return handlerMethods.FirstOrDefault(item => item.Name.Equals(name));
        }

        #endregion
    }

    public class FakeHandler
    {
        public void Handler()
        {
            Console.Write("错误，不能没有参数");
        }

        public void Handler2(DomainEvent<string> domainEvent)
        {
            Console.Write("错误");
        }

        public void Handler3(object data)
        {
            Console.Write("正确");
        }
        
        public void Handler4(DomainEvent<string> domainEvent,object data2)
        {
            Console.Write("错误，参数超过1个");
        }
    }
}
